<template>
  <div class="app-container calendar-list-container">
    <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="订单初审" name="first" v-if="per === '1' || per === '3'">
        <div class="filter-container">
          <component :is="hasList" :list="list" :listLoading="listLoading"></component>
        </div>
      </el-tab-pane>
      <el-tab-pane label="订单复审" name="second" v-if="per === '2' || per === '3'">
        <div class="filter-container">
          <component :is="hasList" :list="list" :listLoading="listLoading"></component>
        </div>
      </el-tab-pane>
    </el-tabs>
    <div v-show="!listLoading" class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page"
        :page-sizes="[5,10,20,30, 50]" :page-size="listQuery['per-page']" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>

  </div>
</template>

<script>
import { firstHasList, secondHasList } from '@/api/order'
import waves from '@/directive/waves' // 水波纹指令
import haslist from './components/haslist' // 订单列表页
import { hasOrderPermission } from '@/utils'

export default {
  name: 'complexTable',
  directives: {
    waves
  },
  data() {
    return {
      hasList: haslist,
      activeName: 'first',
      tableKey: 0,
      list: null,
      total: null,
      per: '3',
      listLoading: true,
      listQuery: {
        page: 1,
        'per-page': 10,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      importanceOptions: [1, 2, 3],
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published'
      },
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    // 获取初审复审权限
    this.per = hasOrderPermission()
    if (this.per === '1') {
      this.activeName = 'first'
    } else if (this.per === '2') {
      this.activeName = 'second'
    } else if (this.per === '3') {
      this.activeName = 'first'
    }
    this.switchTag()
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      this.handleTag() // 状态管理，记录是使用的哪个tag页
      // 判断是待初审还是待复审
      if (this.activeName === 'first') {
        firstHasList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      } else {
        secondHasList(this.listQuery).then(response => {
          this.list = response.data.data
          this.total = response.data._meta.totalCount
          this.listLoading = false
        })
      }
    },
    // 记录使用的是哪个tag页
    handleTag() {
      this.$store.dispatch('setOrderTag', this.activeName)
    },
    // 切换tag页，根据状态记录判断是哪个tag页
    switchTag() {
      if (this.$store.getters.orderTag !== undefined && this.$store.getters.orderTag !== '') {
        this.activeName = this.$store.getters.orderTag
      }
    },
    handleClick() {
      this.resetTemp()
      this.getList()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery['per-page'] = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    }
  }
}
</script>
